﻿Public Class Guide

    Private Sub Guide_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim xmlDatadoc As New XmlDataDocument()
            xmlDatadoc.DataSet.ReadXml("station_info.xml")
            Dim ds As New DataSet("NewStationDataSet")
            ds = xmlDatadoc.DataSet
            DataGridView1.DataSource = ds '.DefaultViewManager
            DataGridView1.Sort(DataGridView1.Columns(3), System.ComponentModel.ListSortDirection.Ascending)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK)
        End Try
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim stringValue As String = Nothing
        ''Handle the More Info button click event
        If e.ColumnIndex = DataGridView1.Columns("more_info").Index Then
            stringValue = DataGridView1.Rows(e.RowIndex).Cells("youwillhear").Value.ToString
            If stringValue Is Nothing Then Return
            MessageBox.Show(stringValue, "- SiNet Station Info -", MessageBoxButtons.OK)
            Return
        End If
        ''Handle the Official Site click
        If e.ColumnIndex = DataGridView1.Columns("url_link").Index Then
            stringValue = DataGridView1.Rows(e.RowIndex).Cells("url").Value.ToString
            If stringValue.Contains("http://") Then
                Process.Start(stringValue)
            Else
                MessageBox.Show("Sorry, Invalid URL.", "- SiNet Error -", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            Return
        End If
        ''Handle the Listen click
        If e.ColumnIndex = DataGridView1.Columns("listen").Index Then
            Dim selectedStation As String = DataGridView1.Rows(e.RowIndex).Cells("station_name").Value.ToString
            Dim theStationIndex As Integer = Nothing
            Dim textItem As String = Nothing
            Dim x As Integer = 0
            If Main.ComboStreamBox.Items.Count > 1 Then
                'If Main.ComboStreamBox.Items.Contains(selectedStation) Then
                For Each textItem In Main.ComboStreamBox.Items
                    If textItem.Contains(selectedStation) Then
                        theStationIndex = x
                    End If
                    x += 1
                Next
                'Dim theStationIndex As Integer = Main.ComboStreamBox.Items.IndexOf(selectedStation)
                If Not theStationIndex = Nothing Then
                    Main.ComboStreamBox.SelectedIndex = theStationIndex
                Else
                    MessageBox.Show("Sorry, I couldn't find " & selectedStation & " in the stream selection box. " & x, "- SiNet Info -", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
                'End If
            Else
                MessageBox.Show("You must log-in before you can listen to " & selectedStation & ".", "- SiNet Info -", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If
    End Sub

    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting

        ''Handle passing the logo URL or Canadian flag to an Image stream
        If DataGridView1.Columns(e.ColumnIndex).Name.Equals("logo") Or DataGridView1.Columns(e.ColumnIndex).Name.Equals("sirca") Then
            '' Ensure that the value is a string.
            Dim stringValue As String = TryCast(e.Value, String)
            If stringValue Is Nothing Or stringValue = "No" Then
                e.Value = New Bitmap(1, 1)
                Return
            ElseIf stringValue = "Yes" Then
                stringValue = "http://www.siriuscanada.ca/en/img/img_cad_lef.gif"
            End If
            '' Replace the string value with the image URL stream.
            e.Value = Image.FromStream(WebRequest.Create(stringValue).GetResponse.GetResponseStream)
        End If
        If DataGridView1.Columns(e.ColumnIndex).Name.Equals("listen") And DataGridView1.Rows(e.RowIndex).Cells("sirca").Value.ToString = "Yes" And My.Settings.sinetCountry = "us" Then
            e.Value = Nothing
        End If
    End Sub
End Class